﻿using System.ComponentModel;

namespace BarrageGameAssistant.Controls
{
    public class HotkeyControl : TextBox
    {
        public Control? FocusControl { get; set; }


        public HotkeyControl() : base() 
        {
            this.ReadOnly = true;
            this.Cursor = Cursors.Hand;
            this.TabStop = false;
            this.BackColor = Color.White;
            
        }

        protected override void OnCreateControl()
        {
            if(string.IsNullOrEmpty(this.PlaceholderText))
                this.PlaceholderText = "请设置按键";

            base.OnCreateControl();
        }

        /// <summary>
        /// 获取快捷键是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return string.IsNullOrEmpty(this.Text);
        }

        /// <summary>
        /// 重置快捷键
        /// </summary>
        public void Reset()
        {
            this.Text = string.Empty;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (((int)(e.KeyData ^ e.Modifiers) == 16) || ((int)(e.KeyData ^ e.Modifiers) == 17) || ((int)(e.KeyData ^ e.Modifiers) == 18))
                return;

            var converter = new KeysConverter();
#pragma warning disable CS8602 // 解引用可能出现空引用。
            this.Text = converter.ConvertToString(e.KeyData).ToUpper();
#pragma warning restore CS8602 // 解引用可能出现空引用。

            if(FocusControl != null)
                FocusControl.Focus();
            base.OnKeyUp(e);
        }
    }
}
